import json

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views.generic import ListView
import hashlib

from UShop.models import TUser,GainAdress,TbPrder
# Create your views here.

##我的页面获取个人信息请求
def mineListView(request, userid):
    geren = TUser.objects.get(user_id=userid)
    data = {}
    data['uaccount'] = geren.uaccount
    data['uname'] = geren.uname
    data['udisplay'] = str(geren.udisplay)
    data['uaddress'] = geren.uaddress
    return JsonResponse(data)


##修改地址的请求
#最简单的实现方法,后面再升级
def updateaddr(request,userid):
    if request.method == 'POST':
        Username = TUser.objects.get(pk=userid)
        address = request.POST.get('address')
        Username.uaddress = address
        Username.save()

        data = {'address':Username.uaddress}

        return JsonResponse(data)
    else:
        return JsonResponse({'error':'修改不成功'})

##删除地址的请求
def addrs(request,userid):
    if request.method == 'POST':
        win = 0
        addressList = GainAdress.objects.filter(id=userid)
        address = request.POST.get('address')
        for addres in addressList:
            if addres.ad_aderss == address:
                addres.delete()
                win = 1

        if win == 1:
            data = {'win': '删除成功'}
            add = GainAdress.objects.filter(id=userid)
            num = 1
            for ad in add:
                data[num] = ad.ad_aderss
                num += 1
            return JsonResponse(data)

        data = {'error':'你要删除的地址不存在,请您重新输入！'}
        num = 1
        for address in addressList:
            data[num] = address.ad_aderss
            num += 1
        return JsonResponse(data)
    else:
        return JsonResponse({'error':'删除地址失败'})

##修改头像的请求,无法测试
def icons(request,userid):
    if request.method == 'POST':
        img = request.FILES['icon']
        use = TUser.objects.get(user_id=userid)
        use.udisplay = img
        use.save()
        return JsonResponse({'win':'修改成功'})

    return JsonResponse({'error':'修改错误'})

##修改昵称的请求
def names(request,userid):
    if request.method == 'POST':
        usename = TUser.objects.get(pk=userid)
        name = request.POST.get('name')
        usename.uname = name
        usename.save()
        data = {'username':usename.uname}
        return JsonResponse(data)
    else:
        return JsonResponse({'error':'修改错误'})

##修改密码的请求
def password(request,userid):
    oldPassword = request.POST.get('password')
    md5 = hashlib.md5()
    md5.update(bytes(oldPassword,encoding='utf-8'))
    oPassword = md5.hexdigest()
    print(oPassword)
    OldUserPassword = TUser.objects.get(pk=userid)
    print(OldUserPassword.upassword)
    if OldUserPassword.upassword == oPassword:

        newPassword = request.POST.get('newPassword')
        newmd5 = hashlib.md5()
        newmd5.update(bytes(newPassword,encoding='utf-8'))
        OldUserPassword.upassword = newmd5.hexdigest()
        OldUserPassword.save()

        data = {'new':'修改成功'}
        return JsonResponse(data)

    else:
        data = {'error':'旧密码输入有误,请重新输入！'}

    return JsonResponse(data)


##退出登录的请求
def logout(request):
    response = HttpResponse('用户注销成功')
    response.delete_cookie('username')
    return response



##获取订单信息请求
def orders(request,userid):
    prderList = TbPrder.objects.filter(user_id=userid,opay_status=1)
    num = 1
    data = {}
    for prde in prderList:
        data[num] = prde.order_number
        num += 1
    return JsonResponse(data)


##页面登录请求
def login(request):

    username = request.POST.get('username','')
    password = request.POST.get('password','')
    # user = TUser()
    md5 = hashlib.md5()
    md5.update(bytes(password,encoding='utf-8'))
    HPassword = md5.hexdigest()
    if TUser.objects.filter(uaccount=username,upassword=HPassword):
        response = HttpResponse(json.dumps({'user':username,'value':HPassword}))
        response = response.set_cookie(key='username',value=HPassword)
        return response
    data = {'error':'账号或密码错误'}
    return JsonResponse(data)


##注册页面请求
def register(request):

    username = request.POST.get('username','')
    password = request.POST.get('password','')
    user = TUser()
    userna = TUser.objects.filter(uaccount=username)
    if userna.exists():
        data = {'error':'账号已存在'}
        return JsonResponse(data)
    else:
        user.uaccount = username
        md5 = hashlib.md5()
        md5.update(bytes(password,encoding='utf-8'))
        Password = md5.hexdigest()
        user.upassword = Password
        user.user_id = 47
        user.save()
        #返回到前端的一个数据
        response = HttpResponse(json.dumps({'username':username,'valuekey':Password}))
        response.set_cookie('username',Password)
    return response



##待付款的请求
def unpaid(request,userid):
    prderList = TbPrder.objects.filter(user_id=userid,oder_status=1,opay_status=0)
    num = 1
    data = {}
    for prde in prderList:
        data[num] = prde.order_number
        num += 1
    return JsonResponse(data)



##待收货的请求
def waitgoods(request,userid):
    goods_start = TbPrder.objects.filter(user_id=userid,state_of_goods=0,opay_status=1)
    num = 1
    data = {}
    for good in goods_start:
        data[num] = good.order_number
        num += 1

    return JsonResponse(data)


##地址管理的请求
def addrss(request,userid):
    useList = GainAdress.objects.filter(id=userid)
    num = 1
    data = {}
    for add in useList:
        data[num] = add.ad_aderss
        num += 1
    return JsonResponse(data)



##添加地址的请求
def addaddr(request,userid):
    if request.method == 'POST':
        add = request.POST.get('address','')
        if add:
            user = TUser.objects.get(pk=userid)
            ADaddress = GainAdress.objects.create(pk=43,id=user,ad_aderss=add)
            ADaddress.save()
            data = {'win':'添加成功'}
            return JsonResponse(data)
    else:
        return JsonResponse({'error': '添加地址失败'})